$(function () {

  //异常分布列表  分页功能
  var anomalousDistributionPageNum = 1;
  var anomalousDistributionPageNumCurr = 1;
  var trouble = '';
  // 异常分布列表 显示
  $('#exceptionViolationModal .formBox .smallTitle i').on("click", function (event) {
    event.preventDefault();
    $(".anomalousDistribution-pop").addClass("is-visible3");
    var n = 1;
    $('#anomalousDistributionPageBox .pageJump input').val('');
    $('#anomalousDistributionModal .searchInputStyle').val('');
    console.log($(this).attr('trouble'));
    trouble = +$(this).attr('trouble') + 1;
    anomalousDistributionPageList(n, trouble, searchContent);
    // $.ajax({
    //   url:'/building/getExceptionDistribution?trouble='+$(this).attr('trouble')+'&page=0&size=10',
    //   success:function(res){
    //     console.log(res);
    //   }
    // });
    var text = $(this).parent().text();
    console.log(text)
    $('#anomalousDistributionExport .tableTitle').text(text);
    // 渲染导出功能的列表
    var obj = {};
    obj.size = 10000;
    obj.page = n - 1;
    obj.trouble = trouble;
    if (searchContent) {
      obj.queryContent = searchContent;
    }
    var ret = getExceptionDistribution(obj);
    ret.then(function (data) {
      console.log(data);
      var content = data.data.content;
      anomalousDistributionPageNum = data.data.totalPages;
      size = data.data.size;
      $('#anomalousDistributionExport tbody').html('');
      var item, reportingTime, describeInfo,duration;
      for (var i = 0; i < content.length; i++) {
        item = content[i].buildingInspectionRecord;
        if(item.reportingTime){
          reportingTime = moment(item.reportingTime).format('YYYY-MM-DD HH:mm');
        }else{
          reportingTime = '空';
        }
        
        describeInfo = item.describeInfo || '';
        duration = item.duration||'';
        $('#anomalousDistributionExport tbody').append('<tr><td>' + ((n - 1) * size + i + 1) + '</td><td>' + item.address + '</td><td>' + item.numberOfFloor + '</td><td>' + item.reportPerson + '</td><td>' + reportingTime + '</td><td>' + duration + '</td><td>' + describeInfo + '</td></tr>');
      }
      $("#anomalousDistributionExport caption").remove();
      $("#anomalousDistributionExport").tableExport({
        formats: ["xlsx"]
      });
    }).catch(function (err) {
      console.log(err);
    });


    // $.ajax({
    //   url: '/deviceStatus/data/list/bydevicetype?deviceTypeId=1',
    //   success: function (res) {
    //     $('#anomalousDistributionExport tbody').html('');
    //     var data = res.data;
    //     for (var i = 0; i < data.length; i++) {
    //       var deviceName = data[i].device.deviceName ? data[i].device.deviceName : '';
    //       var usestatus = data[i].device.usestatus ? data[i].device.usestatus : '';
    //       var deviceTypeName = data[i].device.deviceType.deviceTypeName ? data[i].device.deviceType.deviceTypeName : '';
    //       var devCode = data[i].device.devCode ? data[i].device.devCode : '';
    //       var address = data[i].device.address ? data[i].device.address : '';
    //       var fixedPlace = data[i].device.fixedPlace ? data[i].device.fixedPlace : '';
    //       var latitude = data[i].device.latitude ? data[i].device.latitude : '';
    //       var longitude = data[i].device.longitude ? data[i].device.longitude : '';
    //       var installationDate = data[i].device.installationDate;
    //       var deadline = data[i].device.deadline;

    //       var linkMain1 = data[i].device.linkMain1 ? data[i].device.linkMain1 : '';
    //       var linkPhone1 = data[i].device.linkPhone1 ? data[i].device.linkPhone1 : '';
    //       var linkMain2 = data[i].device.linkMain2 ? data[i].device.linkMain2 : '';
    //       var linkPhone2 = data[i].device.linkPhone2 ? data[i].device.linkPhone2 : '';
    //       var linkMain3 = data[i].device.linkMain3 ? data[i].device.linkMain3 : '';
    //       var linkPhone3 = data[i].device.linkPhone3 ? data[i].device.linkPhone3 : '';
    //       var remark = data[i].device.remark ? data[i].device.remark : '';

    //       installationDate = installationDate ? moment(installationDate).format("YYYY-MM-DD") : '';
    //       deadline = deadline ? moment(installationDate).format("YYYY-MM-DD") : '';
    //       $('#anomalousDistributionExport tbody').append('<tr><td>' + deviceName + '</td><td>' + usestatus + '</td><td>' + deviceTypeName + '</td><td>' + devCode + '</td><td>' + address + '</td><td>' + fixedPlace + '</td><td>' + latitude + '</td><td>' + longitude + '</td><td>' + installationDate + '</td><td>' + deadline + '</td><td>' + linkMain1 + '</td><td>' + linkPhone1 + '</td><td>' + linkMain2 + '</td><td>' + linkPhone2 + '</td><td>' + linkMain3 + '</td><td>' + linkPhone3 + '</td><td>' + data[i].device.remark + '</td></tr>')
    //     }
    //     // $('.anomalousDistributionExportBtn .export').hide();
    //     $("#anomalousDistributionExport caption").remove();
    //     $("#anomalousDistributionExport").tableExport({
    //       formats: ["xlsx"]
    //     });
    //   }
    // });
  });

  function anomalousDistributionPageFunc(pageSum, startPageNum) {
    console.log(pageSum);
    if (pageSum <= 1) {
      $('#anomalousDistributionPageBox .pageJump').hide()
    } else {
      $('#anomalousDistributionPageBox .pageJump').show()
    }
    Page({
      num: pageSum, //页码数
      startnum: startPageNum, //指定页码
      elem: $('#anomalousDistributionPage'), //指定的元素
      callback: function (n) { //回调函数
        anomalousDistributionPageNumCurr = n;
        // $('#earlyWarningPageBox .pageJump').show();
        anomalousDistributionPageList(n, trouble, searchContent);
        // 当翻到下一页时 清除掉 全选按钮的 选中状态

      }
    });
  }

  // 渲染 异常分布列表 的列表
  function anomalousDistributionPageList(n, trouble, searchContent) {
    var obj = {};
    obj.size = 12;
    obj.page = n - 1;
    obj.trouble = trouble;
    if (searchContent) {
      obj.queryContent = searchContent;
    }
    var ret = getExceptionDistribution(obj);
    ret.then(function (data) {
      console.log(data);
      var content = data.data.content;
      anomalousDistributionPageNum = data.data.totalPages;
      size = data.data.size;
      $('#anomalousDistributionModal .tbodyList').html('');
      var item, reportingTime, describeInfo,duration;
      for (var i = 0; i < content.length; i++) {
        item = content[i].buildingInspectionRecord;
        // reportingTime = moment(item.reportingTime).format('YYYY-MM-DD HH:mm');
        if(item.reportingTime){
          reportingTime = moment(item.reportingTime).format('YYYY-MM-DD HH:mm');
        }else{
          reportingTime = '空';
        }
        describeInfo = item.describeInfo || '';
        duration = item.duration||'';
        $('#anomalousDistributionModal .tbodyList').append('<tr><td>' + ((n - 1) * size + i + 1) + '</td><td>' + item.address + '</td><td>' + item.numberOfFloor + '</td><td>' + item.reportPerson + '</td><td>' + reportingTime + '</td><td>' + duration + '</td><td>' + describeInfo + '</td></tr>');
      }
      anomalousDistributionPageFunc(anomalousDistributionPageNum, n);
    }).catch(function (err) {
      console.log(err);
    });
  }

  // 异常分布列表 关闭窗口
  $(".anomalousDistribution-pop").on("click", function (event) {
    if (
      $(event.target).is(".cd-popup-close") ||
      $(event.target).is(".anomalousDistribution-pop")
    ) {
      event.preventDefault();
      $(this).removeClass("is-visible3");
    }
  });
  // 小叉 关闭窗口    
  $('.anomalousDistribution-pop .closeModal').on('click', function (event) {
    event.preventDefault();
    $('.anomalousDistribution-pop').removeClass('is-visible3');
  });

  // 异常分布列表模糊搜索 
  var searchContent = '';
  searchFunc();
  function searchFunc() {
    //模糊查询
    $('body').on('click', function () {
      $("#anomalousDistributionModal .searchList").hide();
    });
    var fn = _.debounce(function () {
      $("#anomalousDistributionModal .searchList").show();
      // hide2border();
      if ($("#anomalousDistributionModal .searchList li")) { //键盘抬起后就判断页面中是否存在这个元素,存在就删除
        $("#anomalousDistributionModal .searchList li").remove();
      }
      searchContent = $("#anomalousDistributionModal .devAddress").val();
      searchContent = $.trim(searchContent);
      console.log(searchContent);
      if (!searchContent) {
        $("#anomalousDistributionModal .searchList").html('');
      }

      //查询内容存在则搜索
      if (searchContent) {
        $.ajax({
          // url: "/building/getExceptionDistribution?trouble=" + trouble + "&queryContent=" + searchContent + "&page=0&size=1000",
          url:'/building/getExceptionDistributionSearch?queryContent='+searchContent,
          type: "GET",
          success: function (result) {
            console.log(result)
            var data = result.data;
            $("#anomalousDistributionModal .searchList").html('');
            for (var i = 0; i < data.length; i++) {
              $("#anomalousDistributionModal .searchList").append('<li>' + data[i] + '</li>');
            }

          }
        });
      }
      $("#anomalousDistributionModal .searchList").show();
    }, 500);
    $("#anomalousDistributionModal .devAddress").on("input", function (e) {
      fn();
    });
    //直接输入地址,点击列表查询
    $("#anomalousDistributionModal .searchList").on("click", "li", function () {
      $("#anomalousDistributionModal .searchList").show();
      var liVal = $(this).text();
      liVal = $.trim(liVal);
      var searchVal = $("#anomalousDistributionModal .devAddress").val(liVal);

      $("#anomalousDistributionModal .searchList").html(""); //hide
      return false;
    });
  }
  // 点击异常分布列表搜索按钮
  var anomalousClickSearchFn = _.debounce(function (){
    anomalousDistributionPageList(1, trouble, searchContent);
  },500);
  $('#anomalousDistributionsearchBtn').on('click', function () {
    anomalousClickSearchFn();
  });


});